<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="箱子id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入箱子id"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="盒子名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入盒子名称"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="盒子状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择盒子状态">
          <el-option
            v-for="dict in dict.type.box_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="盒子类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择盒子类型">
          <el-option
            v-for="dict in dict.type.box_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序值" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序值"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="盒子价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入盒子价格"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="添加时间" prop="addtime">
        <el-input
          v-model="queryParams.addtime"
          placeholder="请输入添加时间"
          clearable
          
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:goods:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:goods:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:goods:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:goods:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="goodsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="箱子ID" align="center" prop="id" />
      <!-- <el-table-column label="分类id" align="center" prop="cId" /> -->
      <el-table-column label="盒子名称" align="center" prop="title" />
      <el-table-column label="盒子封面图" align="center" prop="imgurl">
        <template slot-scope="scope">
          <el-image  fit="cover" :src="scope.row.imgurl" style="width: 80px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="排序值" align="center" prop="sort" />
      <el-table-column label="盒子状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 1">上架</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 2">下架</el-tag>
          <el-tag type="warning" v-if="scope.row.status == 3">售罄</el-tag>
          <el-tag type="warning" v-if="scope.row.status == 4">删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="盒子价格" align="center" prop="price" />
      <el-table-column label="保底数量" align="center" prop="minNum" />
      <el-table-column label="盒子类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.type == 1">普通</el-tag>
          <el-tag type="danger" v-if="scope.row.type == 2">积分</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否全收" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.type == 1">全收</el-tag>
          <el-tag type="danger" v-if="scope.row.type == 2">不开启</el-tag>
        </template>
      </el-table-column> -->

      <el-table-column label="购买方式" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag
            :type="
              item == 1
                ? 'success'
                : item == 2
                ? 'warning'
                : item == 3
                ? 'info'
                : ''
            "
            v-for="item in scope.row.payType"
            :key="item"
          >
            {{
              item == 1 ? "余额" : item == 2 ? "积分" : item == 3 ? "流水" : ""
            }}
          </el-tag>
          <div v-if="scope.row.payType.indexOf('3') != -1">
            {{ scope.row.menKan }}
          </div>

          <!-- <svg-icon v-if="scope.row.payType == 1" icon-class="wechat" /> -->
          <!-- <svg-icon v-if="scope.row.payType == 2" icon-class="money" />
          <svg-icon v-if="scope.row.payType == 3" icon-class="logininfor" /> -->
        </template>
      </el-table-column>
      <el-table-column
        label="产生积分"
        align="center"
        prop="xgAll"
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.ifJf == 0">否</el-tag>
          <el-tag v-else>是</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="产生流水"
        align="center"
        prop="xgAll"
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.ifLs == 0">否</el-tag>
          <el-tag v-else>是</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="添加时间"
        align="center"
        prop="addtime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
          scope.row.createDate
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updatetime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.updateTime, "{y}-{m}-{d} {hh}:{mm}:{ss}")
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建人" align="center" prop="type">
        <template slot-scope="scope">
          <span>{{ scope.row.creator }}</span>
        </template>
      </el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:goods:edit']"
            >一键导出</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:goods:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listGoods,
  getGoods,
  delGoods,
  addGoods,
  updateGoods,
} from "@/api/wxs/box.js";
import { getToken } from "@/utils/auth";
export default {
  dicts: ["box_status", "box_type", "pay_type"],
  name: "Goods",
  watch: {
    //监听支付方式
    "form.payType": function (val) {
      console.log(val);
      //判断数组是否有3
      if (val) {
        if (val.indexOf("3") != -1) {
          this.if_menkan = true;
        } else {
          this.if_menkan = false;
        }
      }
    },
  },
  data() {
    return {
      if_menkan: false,
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/admin/uploadFile",
        // 上传的文件列表
        fileList: [],
      },

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盒子信息表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        isAsc: "desc", // 排序，asc／desc
        orderByColumn: "updateTime",

        pageNum: 1,
        pageSize: 10,
        cId: null,
        title: null,
        imgurl: null,
        sort: null,
        status: null,
        price: null,
        minNum: null,
        type: null,
        addtime: null,
        creator: null,
        createDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cId: [{ required: true, message: "分类id不能为空", trigger: "blur" }],
        title: [
          { required: true, message: "盒子名称不能为空", trigger: "blur" },
        ],
        imgurl: [
          { required: true, message: "盒子封面图不能为空", trigger: "blur" },
        ],
        sort: [{ required: true, message: "排序值不能为空", trigger: "blur" }],
        status: [
          {
            required: true,
            message: "1上架 2 下架 3删除不能为空",
            trigger: "blur",
          },
        ],
        price: [
          { required: true, message: "盒子价格不能为空", trigger: "blur" },
        ],
        minNum: [
          { required: true, message: "保底数量不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "1普通 2积分不能为空", trigger: "change" },
        ],
        addtime: [
          { required: true, message: "添加时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    console.log(this.$store.getters);
  },
  methods: {
    handleFileUploadProgress(e) {
      this.form.imgurl = e;
    },
    /** 查询盒子信息列表 */
    async getList() {
      this.loading = true;
      await listGoods(this.queryParams).then((response) => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      //转化支付方式数组
      this.goodsList.forEach((item, index) => {
        if (item.payType) {
          this.goodsList[index].payType =
            this.goodsList[index].payType.split(",");
        } else {
          item.payType = [];
        }
      });
      console.log(this.goodsList);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cId: null,
        title: null,
        imgurl: null,
        sort: null,
        status: 0,
        price: null,
        minNum: null,
        type: null,
        addtime: null,
        updateTime: null,
        creator: null,
        createDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加盒子信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGoods(id).then((response) => {
        this.form = response.data;
        console.log(this.form);
        //发送数据到父组件
        this.$emit("func", this.form);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.payType = `${this.form.payType}`;
          if (this.form.id != null) {
            updateGoods(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.creator = this.$store.getters.id;
            addGoods(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除盒子信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delGoods(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/goods/export",
        {
          ...this.queryParams,
        },
        `goods_${new Date().getTime()}.xlsx`
      );
    },
    /** 添加商品 */
    handleEditTable(row) {
      const tableId = row.id;
      const tableName = row.title;
      //   const params = { pageNum: this.queryParams.pageNum };
      console.log(row);
      this.$tab.openPage(
        "添加[" + tableName + "]盒子商品",
        "/wxs/add_good/index/" + tableId
      );
    },
  },
};
</script>
